﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Toenda.Foundation.Data {
	/// <summary>
	/// Class FlatfileExport
	/// </summary>
	public class FlatfileExport {
		/// <summary>
		/// Self
		/// </summary>
		public static FlatfileExport Self = new FlatfileExport();

		private StreamWriter _sw;

		// ---------------------------------------------------
		// CONSTRUCTORS
		// ---------------------------------------------------

		/// <summary>
		/// Default Ctor
		/// </summary>
		private FlatfileExport() {
		}

		// ---------------------------------------------------
		// INTERFACE IMPLEMENTATIONS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PROPERTIES
		// ---------------------------------------------------

		// ---------------------------------------------------
		// EVENTS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PRIVATE MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Open a filestream
		/// </summary>
		/// <param name="filename">The name including path of the file</param>
		/// <param name="append">True if you want to append to the file</param>
		private void _OpenFile(string filename, bool append) {
			try {
				this._sw = new StreamWriter(
					filename,
					append
				);
			}
			catch(Exception ex) {
				throw new Exception(
					"FileStream Object encoured an error!",
					ex
				);
			}
		}

		/// <summary>
		/// Close the open file
		/// </summary>
		private void _CloseFile() {
			try {
				this._sw.Close();
				this._sw.Dispose();
				this._sw = null;
			}
			catch(Exception ex) {
				throw new Exception(
					"FileStream Object is not initialized, please open the file first!",
					ex
				);
			}
		}

		/// <summary>
		/// Write some text to a file (only with manually open and close)
		/// </summary>
		/// <param name="text"></param>
		private void _WriteToFile(string text) {
			if(this._sw == null) {
				throw new Exception("Please open a file at first!");
			}

			this._sw.WriteLine(text);
		}

		// ---------------------------------------------------
		// PROTECTED MEMBERS
		// ---------------------------------------------------

		// ---------------------------------------------------
		// PUBLIC MEMBERS
		// ---------------------------------------------------

		/// <summary>
		/// Export a list
		/// </summary>
		/// <param name="dataSource"></param>
		/// <param name="fileName"></param>
		public FlatfileExportReturnValue ExportDataList(
			List<FlatfileExportDataContainer> dataSource,
			string fileName
			) {
			try {
				_OpenFile(fileName, true);

				int cellCount = 0;
				int rowCount = 0;

				if(dataSource.Count > 0) {
					cellCount = dataSource[0].Cells.Count;

					if(dataSource[0].Columns.Count > 0) {
						cellCount++;
					}
				}

				rowCount = dataSource.Count;

				if(dataSource.Count > 0) {
					if(dataSource[0].Columns.Count > 0) {
						rowCount++;
					}
				}

				StringBuilder str = new StringBuilder();
				bool isFirst = true;
				int count = 1;
				//int colCount = 0;

				foreach(FlatfileExportDataContainer row in dataSource) {
					if(isFirst) {
						str.Remove(0, str.Length);

						foreach(string value in row.Columns) {
							count++;

							str.Append(value);

							if(count < cellCount) {
								str.Append(";");
							}
						}

						_WriteToFile(str.ToString());
					}

					count = 0;

					str.Remove(0, str.Length);

					foreach(string value in row.Cells) {
						count++;

						str.Append(value);

						if(count < cellCount) {
							str.Append(";");
						}
					}

					_WriteToFile(str.ToString());

					isFirst = false;
				}

				_CloseFile();

				return FlatfileExportReturnValue.Successfull;
			}
			catch(Exception) {
				return FlatfileExportReturnValue.DefaultExportError;
			}
		}
	}

	/// <summary>
	/// Class FlatfileExportDataContainer
	/// </summary>
	public class FlatfileExportDataContainer {
		private List<string> _columns;
		private List<string> _cell;

		/// <summary>
		/// Default Ctor
		/// </summary>
		public FlatfileExportDataContainer() {
			this._columns = new List<string>();
			this._cell = new List<string>();
		}

		/// <summary>
		/// Get the columns
		/// </summary>
		public List<string> Columns {
			get { return this._columns; }
		}

		/// <summary>
		/// Get the cells
		/// </summary>
		public List<string> Cells {
			get { return this._cell; }
		}
	}
}
